/**
 * Created by wangzilong on 2017/4/12.
 */
const test = 'http://test.520m.com.cn/api/';
const project = 'http://www.520m.com.cn/api/';
var serviceUrl = test;

function MessageQueue(serviceName, dataObj, call, method) {
    // alert(url+urlMsg)
    if(method){
        var list = [];
        var str = '';
        for(var key in dataObj){
            if(key=='access'){
                str = 'access-token='+dataObj[key];
                // list.push(key+'-token='+dataObj[key])
            }else{
                list.push(key+'='+dataObj[key]);
            }
        }
        var urlMsg =list.join('&');
        var url = serviceUrl + serviceName +str;
        fetch(url,{
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body:urlMsg
        })
        .then((response)=>{
            // 
            return response.text();
        })
        .then((data)=>{ 
            // alert(data)
            var obj = JSON.parse(data);
            call(obj);
        })
        .catch((error)=>{
            console.log('Request Faild', error);
        })
    }else{
        var list = [];
        for(var key in dataObj){
            if(key=='access'){
                list.push(key+'-token='+dataObj[key])
            }else{
                list.push(key+'='+dataObj[key]);
            }
        }
        var urlMsg =list.join('&');
        var url = serviceUrl + serviceName;
        fetch(url+urlMsg)
        .then((response)=>{
            // 
            return response.text();
        })
        .then((data)=>{ 
            // alert(data)
            var obj = JSON.parse(data);
            call(obj);
        })
        .catch((error)=>{
            // alert(JSON.stringify(error))
            console.log('Request Faild', error);
        })
    }
    
}

module.exports = MessageQueue;

